package pw.hais.main.main_app.dao;

import com.squareup.okhttp.Response;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

import pw.hais.http.Http;
import pw.hais.http.base.OnHttpListener;
import pw.hais.main.main_app.enums.HaiHanType;
import pw.hais.main.main_app.model.NeiHan8ArticleModel;
import pw.hais.utils.L;

/**
 * 采集 http://www.neihan8.com
 * Created by Hais1992 on 2015/12/16.
 */
public class NeiHan8Dao {
    public static final String SERVER_URL_Article = "http://www.neihan8.com/article/";
    public static final String SERVER_URL_Image = "http://www.neihan8.com";

    /** 获取列表 */
    public static void getList(HaiHanType type, final String page, final OnHttpListener<List<NeiHan8ArticleModel>> listener) {
        switch (type){
            case Article:
                String article_url = "1".equals(page)?SERVER_URL_Article:SERVER_URL_Article + "index_" + page + ".html";
                Http.get(article_url, null, new OnHttpListener<String>() {
                    @Override
                    public void onSuccess(Response response, String html) {
                        doArticleHtmlToListModel(html, listener);
                    }
                });
                break;
            case Images:
                String images_url = "1".equals(page)?SERVER_URL_Image+"/tupian/":SERVER_URL_Image+"/tupian/" + "index_" + page + ".html";
                Http.get(images_url, null, new OnHttpListener<String>() {
                    @Override
                    public void onSuccess(Response response, String html) {
                        doImagesHtmlToListModel(html, listener);
                    }
                });
                break;
        }
    }


    /** 获取内容详情 */
    public static void getModelById(String type, final String id, final OnHttpListener<NeiHan8ArticleModel> listener){
       if(HaiHanType.Article.string.equals(type)){
           Http.get(SERVER_URL_Article + id + ".html", null, new OnHttpListener<String>() {
               @Override
               public void onSuccess(Response response, String html) {
                   doArticleHtmlToItemModel(id,html, listener);
               }
           });
       }else if(HaiHanType.Images.string.equals(type)){
           Http.get(SERVER_URL_Image + id, null, new OnHttpListener<String>() {
               @Override
               public void onSuccess(Response response, String html) {
                   doImagesHtmlToItemModel(id,html, listener);
               }
           });
       }

    }





    private static void doImagesHtmlToListModel(String html, OnHttpListener<List<NeiHan8ArticleModel>> listener) {
        List<NeiHan8ArticleModel> list = new ArrayList<>();
        Document document = Jsoup.parse(html);
        Elements elements = document.getElementsByClass("pic-column-item");
        for (Element e : elements) {
            Element title_element = e.select("h3").select("a").first();
            String id = title_element.select("a").attr("href");
            String title = title_element.text();
            String image_url = e.getElementsByClass("img").select("img").attr("src");
            list.add(new NeiHan8ArticleModel(id, title, title,image_url));
        }
        if (listener != null) listener.onSuccess(null, list);
    }




    private static void doArticleHtmlToListModel(String html, OnHttpListener<List<NeiHan8ArticleModel>> listener) {
        List<NeiHan8ArticleModel> list = new ArrayList<>();
        Document document = Jsoup.parse(html);
        Elements elements = document.getElementsByClass("text-column-item");
        for (Element e : elements) {
            Element title_element = e.getElementsByClass("title").first();
            String id = title_element.select("a").attr("href").replace("/article/", "").replace(".html", "");
            String title = title_element.text();
            String content = e.getElementsByClass("desc").text();
            list.add(new NeiHan8ArticleModel(id, title, content));
        }
        if (listener != null) listener.onSuccess(null, list);
    }




    private static void doArticleHtmlToItemModel(String id,String html, OnHttpListener<NeiHan8ArticleModel> listener){
        Document document = Jsoup.parse(html);
        Element element = document.getElementsByClass("page-detail").first();

        String title = element.getElementsByClass("title").text();

        Element es_content = element.getElementsByClass("detail").first();
        es_content.select(".art_newding").remove();
        es_content.select(".ad610").remove();
        String content = es_content.html();

        listener.onSuccess(null,new NeiHan8ArticleModel(id,title,content));
    }


    private static void doImagesHtmlToItemModel(String id,String html, OnHttpListener<NeiHan8ArticleModel> listener){
        Document document = Jsoup.parse(html);
        Element element = document.getElementsByClass("page-detail").first();

        String title = element.getElementsByClass("title").text();

        Element es_content = element.getElementsByClass("detail").first();
        String image_url = es_content.select("p").select("a").select("img").attr("src");

        listener.onSuccess(null,new NeiHan8ArticleModel(id,title,title,image_url));
    }
}
